package com.younion.spring;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.younion.beans.TabRefBean;
import com.younion.business.ICardAgioService;
import com.younion.business.ICardCountInitService;
import com.younion.business.ICardEntityService;
import com.younion.business.ICardLevelService;
import com.younion.business.ICommonDelService;
import com.younion.business.ICostService;
import com.younion.business.ICustomerService;
import com.younion.business.IMembercardServiceService;
import com.younion.business.IMsgTemplateService;
import com.younion.business.IPermissionsService;
import com.younion.business.IProductKindService;
import com.younion.business.IRechargeRuleService;
import com.younion.business.IRegioninfoService;
import com.younion.business.IRolesService;
import com.younion.business.IServiceInitService;
import com.younion.business.IUpgradeRuleService;
import com.younion.common.constants.Status;
import com.younion.common.constants.SysConstants;
import com.younion.common.constants.TemplateTargetType;
import com.younion.common.exception.BaseException;
import com.younion.permission.PermissionUtil;
import com.younion.vo.ActionResultJsonVO;
import com.younion.vo.CardAgioVO;
import com.younion.vo.CardCountInitVO;
import com.younion.vo.CardEntityVO;
import com.younion.vo.CardLevelVO;
import com.younion.vo.CostVO;
import com.younion.vo.CustomerVO;
import com.younion.vo.MemberCardVO;
import com.younion.vo.MembercardServiceVO;
import com.younion.vo.MsgTemplateVO;
import com.younion.vo.PermissionsVO;
import com.younion.vo.ProductKindVO;
import com.younion.vo.ProductVO;
import com.younion.vo.RechargeRuleVO;
import com.younion.vo.RegioninfoVO;
import com.younion.vo.RolesVO;
import com.younion.vo.ServiceInitVO;
import com.younion.vo.UpgradeRuleVO;
import com.younion.vo.UserVO;

import cpcns.jhsks.util.MapCollectionUtils;

@Controller
public class AjaxController implements ApplicationContextAware {
	
	private ApplicationContext applicationContext;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	@Autowired
	private IRolesService rolesService = null;

	@Autowired
	private IPermissionsService permissionsService = null;

	@Autowired
	private ICustomerService customerService = null;

	@Autowired
	private ICardCountInitService cardCountInitService = null;

	@Autowired
	private IMsgTemplateService msgTemplateService = null;

	@Autowired
	private IServiceInitService serviceInitService = null;

	@Autowired
	private IRechargeRuleService rechargeRuleService = null;

	@Autowired
	private IUpgradeRuleService upgradeRuleService = null;

	@Autowired
	private ICardLevelService cardLevelService = null;

	@Autowired
	private ICardAgioService cardAgioService = null;
	@Autowired
	private IRegioninfoService regioninfoService = null;
	@Autowired
	private ICostService costService = null;

	@Autowired
	private IMembercardServiceService membercardServiceService = null;
	@Autowired
	private IProductKindService productKindService = null;
	@Autowired
	private ICardEntityService cardEntityService = null;
	@Autowired
	private ICommonDelService commonDelService = null;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@RequestMapping("/ajax/getPermissionSelection")
	public @ResponseBody
	Map<String, List<PermissionsVO>> getPermissionSelection(
			@RequestParam("roleId")
			String roleId) {
		Map<String, List<PermissionsVO>> permissionsMap = Maps.newHashMap();
		List<PermissionsVO> selected = Lists.newArrayList();
		List<PermissionsVO> avalible = Lists.newArrayList();
		try {
			List<PermissionsVO> all = permissionsService.getAll();
			if (roleId != null) {
				RolesVO role = rolesService.getById(roleId);
				if (role != null && role.getPermissions() !=null) {
					selected = permissionsService.selectByIds(Arrays
							.asList(role.getPermissions().split(
									PermissionUtil.comma)));
					for (PermissionsVO p : all) {
						if (!selected.contains(p.getId())) {
							avalible.add(p);
						}
					}
				} else {
					avalible.addAll(all);
				}
			} else {
				avalible.addAll(all);
			}
			permissionsMap.put("available", avalible);
			permissionsMap.put("selected", selected);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return permissionsMap;
	}

	@RequestMapping("/ajax/lookupCustomers")
	public @ResponseBody
	List<CustomerVO> lookupCustomers(@RequestParam("c")
	String ids) {
		List<CustomerVO> customers = Lists.newArrayList();
		try {
			List params = Arrays.asList(ids.split(PermissionUtil.comma));
			customers = customerService.selectByIds(params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return customers;
	}

	@RequestMapping("/ajax/saveCardCountInit")
	public @ResponseBody
	ActionResultJsonVO saveCardCountInit(@RequestParam("cardEntityId")
	Integer cardEntityId, @RequestParam("productId")
	Integer productId, @RequestParam("inputCount")
	Integer inputCount) {
		String message = "操作成功";
		String statusCode = "200";
		ActionResultJsonVO actionResultJsonVO = new ActionResultJsonVO();
		try {
			CardCountInitVO cardCountInitVO = new CardCountInitVO();
			ProductVO productVO = new ProductVO();
			productVO.setId(productId);
			Map paramMap = null;
			if (cardCountInitVO != null) {
				cardCountInitVO.setCardEntityId(cardEntityId);
				cardCountInitVO.setProductVO(productVO);
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
						.describe(cardCountInitVO));
				cardCountInitVO.setCount(inputCount);
			}

			int count = cardCountInitService.getCountByMap(paramMap);
			System.out.println("count:" + count);
			if (count == 0) {
				cardCountInitVO = cardCountInitService.save(cardCountInitVO);
			} else {
				paramMap.put("count", inputCount);
				cardCountInitService.updateCountByMap(paramMap);
			}
		} catch (Exception e) {
			message = "操作失败";
			statusCode = "300";
			e.printStackTrace();
		}
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setMessage(message);
		actionResultJsonVO.setNavTabId("");
		actionResultJsonVO.setRel("");
		actionResultJsonVO.setStatusCode(statusCode);
		return actionResultJsonVO;
	}

	@RequestMapping("/ajax/deleteCardCountInit")
	public @ResponseBody
	ActionResultJsonVO deleteCardCountInit(@RequestParam("cardEntityId")
	Integer cardEntityId, @RequestParam("productId")
	Integer productId) {
		String message = "操作成功";
		String statusCode = "200";
		ActionResultJsonVO actionResultJsonVO = new ActionResultJsonVO();
		try {
			ProductVO productVO = new ProductVO();
			productVO.setId(productId);
			Map paramMap = new HashMap();
			paramMap.put("cardEntityId", cardEntityId);
			paramMap.put("productVO", productVO);
			cardCountInitService.deleteCardCountByMap(paramMap);
		} catch (Exception e) {
			message = "操作失败";
			statusCode = "300";
			e.printStackTrace();
		}
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setMessage(message);
		actionResultJsonVO.setNavTabId("");
		actionResultJsonVO.setRel("");
		actionResultJsonVO.setStatusCode(statusCode);
		return actionResultJsonVO;
	}

	@RequestMapping("/ajax/getServicePlan")
	public @ResponseBody
	Map<String, List<MsgTemplateVO>> getServicePlan(
			@RequestParam("cardEntityId")
			String cardEntityId) {
		Map<String, List<MsgTemplateVO>> msgTemplateVOsMap = Maps.newHashMap();
		List<MsgTemplateVO> selected = Lists.newArrayList();
		List<MsgTemplateVO> avalible = Lists.newArrayList();
		try {
			CardEntityVO cardEntityVO = cardEntityService.getById(cardEntityId);
			List<MsgTemplateVO> all = getAllMsgtemplates(cardEntityVO
					.getCustomerId());
			if (cardEntityId != null) {
				ServiceInitVO serviceInitVO = null;
				Map<String, String> paramMap = Maps.newHashMap();
				paramMap.put("cardEntityId", cardEntityId);
				Collection serviceInits = serviceInitService.getByMap(paramMap);
				Iterator it = serviceInits.iterator();
				while (it.hasNext()) {
					serviceInitVO = (ServiceInitVO) it.next();
					break;
				}
				if (serviceInitVO != null) {
					selected = (List<MsgTemplateVO>) msgTemplateService
							.selectByIds(Arrays
									.asList(serviceInitVO.getTemplate().getId().toString().split(
											PermissionUtil.comma)));
				}
			}
			all.removeAll(selected);
			msgTemplateVOsMap.put("available", all);
			msgTemplateVOsMap.put("selected", selected);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return msgTemplateVOsMap;
	}

	@RequestMapping("/ajax/getMemberCardServicePlan")
	public @ResponseBody
	Map<String, List<MsgTemplateVO>> getMemberCardServicePlan(
			@RequestParam("cardEntityId")
			Integer cardEntityId, @RequestParam("membercardId")
			Integer membercardId) {
		Map<String, List<MsgTemplateVO>> msgTemplateVOsMap = Maps.newHashMap();
		List<MsgTemplateVO> selected = Lists.newArrayList();
		List<MsgTemplateVO> avalible = Lists.newArrayList();
		try {
			CardEntityVO cardEntityVO = cardEntityService.getById(cardEntityId
					.toString());
			List<MsgTemplateVO> all = getAllMsgtemplates(cardEntityVO
					.getCustomerId());
			if (cardEntityId != null) {
				MembercardServiceVO serviceInitVO = null;
				MemberCardVO memberCardVO = new MemberCardVO(membercardId);
				Map paramMap = new HashMap();
				paramMap.put("memberCard", memberCardVO);
				Collection serviceInits = membercardServiceService
						.getByMap(paramMap);
				Iterator it = serviceInits.iterator();
				while (it.hasNext()) {
					serviceInitVO = (MembercardServiceVO) it.next();
					break;
				}
				if (serviceInitVO != null) {
					/*selected = (List<MsgTemplateVO>) msgTemplateService
							.selectByIds(Arrays
									.asList(serviceInitVO.getServiceId().split(
											PermissionUtil.comma)));*/
				}
			}
			all.removeAll(selected);
			msgTemplateVOsMap.put("available", all);
			msgTemplateVOsMap.put("selected", selected);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return msgTemplateVOsMap;
	}

	private List<MsgTemplateVO> getAllMsgtemplates(Integer customerId)
			throws BaseException {
		Map loadMsgtemplateParamMap = Maps.newHashMap();
		loadMsgtemplateParamMap.put("status", Status.ACTIVE.getName());
		loadMsgtemplateParamMap.put("targetType1", TemplateTargetType.BIRTHDAY.getName());
		loadMsgtemplateParamMap.put("targetType2", TemplateTargetType.NOCOST.getName());
		loadMsgtemplateParamMap.put("targetType3", TemplateTargetType.NORECHARGE.getName());
		loadMsgtemplateParamMap.put("targetType4", TemplateTargetType.LESSREMAIN.getName());
		if (getSessionUser().getCustomer() != null) {
			// loadMsgtemplateParamMap.put("customerId",
			// getSessionUser().getCustomer().getId());
			loadMsgtemplateParamMap.put("customerId", customerId);
		}
		List<MsgTemplateVO> all = (List<MsgTemplateVO>) msgTemplateService
				.getByMap(loadMsgtemplateParamMap);
		return all;
	}

	@RequestMapping("/ajax/saveRechargeRule")
	public @ResponseBody
	Map saveRechargeRule(@RequestParam("cardEntityId")
	Integer cardEntityId, @RequestParam("rechargeMoney")
	Double rechargeMoney, @RequestParam("firstGiveMoney")
	Double firstGiveMoney, @RequestParam("givePoint")
	Integer givePoint, @RequestParam("frequency")
	Integer frequency, @RequestParam("everyGiveMoney")
	Double everyGiveMoney, @RequestParam("giveCount")
	Integer giveCount) {
		String message = "操作成功";
		String statusCode = "200";
		ActionResultJsonVO actionResultJsonVO = new ActionResultJsonVO();
		int insertKey = 0;
		try {
			RechargeRuleVO rechargeRuleVO = new RechargeRuleVO();
			Map paramMap = null;
			Map updateParamMap = null;
			rechargeRuleVO.setCardEntityId(cardEntityId);
			rechargeRuleVO.setRechargeMoney(rechargeMoney);
			paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
					.describe(rechargeRuleVO));

			List<RechargeRuleVO> rechargeRuleVOList = (List<RechargeRuleVO>) rechargeRuleService
					.getByMap(paramMap);
			rechargeRuleVO.setFirstGiveMoney(firstGiveMoney);
			rechargeRuleVO.setGivePoint(givePoint);
			rechargeRuleVO.setFrequency(frequency);
			rechargeRuleVO.setEveryGiveMoney(everyGiveMoney);
			rechargeRuleVO.setGiveCount(giveCount);
			updateParamMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
					.describe(rechargeRuleVO));
			if (rechargeRuleVOList != null && rechargeRuleVOList.size() > 0) {
				insertKey = rechargeRuleVOList.get(0).getId();
				rechargeRuleVO.setId(insertKey);
				updateParamMap = MapCollectionUtils
						.removeEmptyEntry(PropertyUtils
								.describe(rechargeRuleVO));
				rechargeRuleService.updateByMap(updateParamMap);
			} else {
				insertKey = rechargeRuleService.saveReturnKey(rechargeRuleVO);
			}
		} catch (Exception e) {
			message = "操作失败";
			statusCode = "300";
			e.printStackTrace();
		}
		System.out.println("insertKey:" + insertKey);
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setMessage(message);
		actionResultJsonVO.setNavTabId("");
		actionResultJsonVO.setRel("");
		actionResultJsonVO.setStatusCode(statusCode);
		Map returnMap = new HashMap();
		returnMap.put("result", actionResultJsonVO);
		returnMap.put("key", insertKey);
		return returnMap;
	}

	@RequestMapping("/ajax/deleteRechargeRule")
	public @ResponseBody
	ActionResultJsonVO deleteRechargeRule(@RequestParam("id")
	Integer id) {
		String message = "操作成功";
		String statusCode = "200";
		ActionResultJsonVO actionResultJsonVO = new ActionResultJsonVO();
		try {
			List ids = new ArrayList();
			ids.add(id);
			rechargeRuleService.remove(ids);
		} catch (Exception e) {
			message = "操作失败";
			statusCode = "300";
			e.printStackTrace();
		}
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setMessage(message);
		actionResultJsonVO.setNavTabId("");
		actionResultJsonVO.setRel("");
		actionResultJsonVO.setStatusCode(statusCode);
		return actionResultJsonVO;
	}

	@RequestMapping("/ajax/saveUpgradeRule")
	public @ResponseBody
	Map saveUpgradeRule(@RequestParam("cardEntityId")
	Integer cardEntityId, @RequestParam("newCardEntityId")
	Integer newCardEntityId, @RequestParam("needPoint")
	Integer needPoint, @RequestParam("deductionPoint")
	Integer deductionPoint) {
		String message = "操作成功";
		String statusCode = "200";
		ActionResultJsonVO actionResultJsonVO = new ActionResultJsonVO();
		int insertKey = 0;
		try {
			UpgradeRuleVO upgradeRuleVO = new UpgradeRuleVO();
			Map paramMap = null;
			Map updateParamMap = null;
			upgradeRuleVO.setOldCardEntityId(cardEntityId);
			paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
					.describe(upgradeRuleVO));
			UpgradeRuleVO old_upgradeRuleVO = upgradeRuleService.getByOldCardId(cardEntityId);
			upgradeRuleVO.setNeedPoint(needPoint);
			upgradeRuleVO.setDeductionPoint(deductionPoint);
			upgradeRuleVO.setNewCardEntityId(newCardEntityId);
			updateParamMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
					.describe(upgradeRuleVO));
			if (old_upgradeRuleVO!=null) {
				insertKey = old_upgradeRuleVO.getId();
				upgradeRuleVO.setId(insertKey);
				updateParamMap = MapCollectionUtils
						.removeEmptyEntry(PropertyUtils.describe(upgradeRuleVO));
				upgradeRuleService.updateByMap(updateParamMap);
			} else {
				insertKey = upgradeRuleService.saveReturnKey(upgradeRuleVO);
			}
		} catch (Exception e) {
			message = "操作失败";
			statusCode = "300";
			e.printStackTrace();
		}
		System.out.println("insertKey:" + insertKey);
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setMessage(message);
		actionResultJsonVO.setNavTabId("");
		actionResultJsonVO.setRel("");
		actionResultJsonVO.setStatusCode(statusCode);
		Map returnMap = new HashMap();
		returnMap.put("result", actionResultJsonVO);
		returnMap.put("key", insertKey);
		return returnMap;
	}

	@RequestMapping("/ajax/deleteUpgradeRule")
	public @ResponseBody
	ActionResultJsonVO deleteUpgradeRule(@RequestParam("id")
	Integer id) {
		String message = "操作成功";
		String statusCode = "200";
		ActionResultJsonVO actionResultJsonVO = new ActionResultJsonVO();
		try {
			List ids = new ArrayList();
			ids.add(id);
			upgradeRuleService.remove(ids);
		} catch (Exception e) {
			message = "操作失败";
			statusCode = "300";
			e.printStackTrace();
		}
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setCallbackType("");
		actionResultJsonVO.setMessage(message);
		actionResultJsonVO.setNavTabId("");
		actionResultJsonVO.setRel("");
		actionResultJsonVO.setStatusCode(statusCode);
		return actionResultJsonVO;
	}

	@RequestMapping("/ajax/ajaxGetLevelByCustomer")
	public @ResponseBody
	List<CardLevelVO> ajaxGetLevelByCustomer(@RequestParam("customerId")
	Integer customerId) {
		Map paramMap = new HashMap();
		CustomerVO cust = new CustomerVO();
		cust.setId(customerId);
		paramMap.put("customerVO", cust);
		List<CardLevelVO> cardLevelList = null;
		try {
			cardLevelList = (List<CardLevelVO>) cardLevelService
					.getByMap(paramMap);
		} catch (BaseException e) {
			e.printStackTrace();
		}
		return cardLevelList;
	}

	@RequestMapping("/ajax/ajaxGetAgioByCustomer")
	public @ResponseBody
	List<CardAgioVO> ajaxGetAgioByCustomer(@RequestParam("customerId")
	Integer customerId) {
		Map paramMap = new HashMap();
		CustomerVO c = new CustomerVO();
		c.setId(customerId);
		paramMap.put("customerVO", c);
		List<CardAgioVO> cardAgioVOList = null;
		try {
			cardAgioVOList = (List<CardAgioVO>) cardAgioService
					.getByMap(paramMap);
		} catch (BaseException e) {
			e.printStackTrace();
		}
		return cardAgioVOList;
	}

	/**
	 * 异步加载所有省份
	 * 
	 * @return
	 */
	@RequestMapping("/ajax/ajaxGetAllProvince")
	public @ResponseBody
	List ajaxGetAllProvince() {
		List<RegioninfoVO> regioninfoList = null;// 结果集列表
		try {
			regioninfoList = regioninfoService.getAllProvince();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return regioninfoList;
	}

	/**
	 * 异步加载指定省份下的所有市
	 * 
	 * @return
	 */
	@RequestMapping("/ajax/ajaxGetAllCity")
	public @ResponseBody
	List ajaxGetAllCity(@RequestParam("pid")
	Integer pid) {
		List<RegioninfoVO> regioninfoList = null;// 结果集列表
		Map paramMap = new HashMap();
		paramMap.put("pid", pid);
		try {
			regioninfoList = (List<RegioninfoVO>) regioninfoService
					.getByMap(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return regioninfoList;
	}

	/**
	 * 异步加载所有制定城市下的所有区
	 * 
	 * @return
	 */
	@RequestMapping("/ajax/ajaxGetArea")
	public @ResponseBody
	List ajaxGetArea(@RequestParam("area")
	String area) {
		List<RegioninfoVO> regioninfoList = null;// 结果集列表
		try {
			regioninfoList = regioninfoService.getRegionByIds(Arrays
					.asList(area.split(",")));
		} catch (Exception e) {
		}
		return regioninfoList;
	}

	@RequestMapping("/ajax/loadLastCostLog")
	public @ResponseBody
	CostVO loadLastCostLog(@RequestParam("cardId")
	Integer cardId, @RequestParam("productId")
	Integer productId) {
		try {
			CostVO param = new CostVO();
			param.setCardEntity(new MemberCardVO(cardId));
			param.setProduct(new ProductVO(productId));
			param.setDescOrAsc("DESC");
			param.setOrderBy("cost_date");
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
					.describe(param));
			paramMap.put("offset", 0);
			paramMap.put("limit", 1);
			Collection costs = this.getCostService().getByMap(paramMap);
			if (costs.size() > 0)
				return (CostVO) (costs.toArray())[0];
		} catch (Exception e) {
		}
		return null;
	}

	@RequestMapping("/ajax/ajaxGetProductKindByCustomer")
	public @ResponseBody
	List<ProductKindVO> ajaxGetProductKindByCustomer(
			@RequestParam("customerId")
			Integer customerId) {
		Map paramMap = new HashMap();
		CustomerVO customer = new CustomerVO(customerId);
		paramMap.put("customerVO", customer);
		List<ProductKindVO> productKindVOList = null;
		try {
			productKindVOList = (List<ProductKindVO>) productKindService
					.getByMap(paramMap);
		} catch (BaseException e) {
			e.printStackTrace();
		}
		return productKindVOList;
	}
	
	@RequestMapping("/ajax/getTemplate")
	public @ResponseBody MsgTemplateVO getTemplate(@RequestParam("id") String id) {
		try {
			return msgTemplateService.getById(id);
		} catch (Exception e) {
			return null;
		}
	}
	
	@RequestMapping("/ajax/ajaxGetCardEntityByCustomer")
	public @ResponseBody Collection ajaxGetCardEntityByCustomer(@RequestParam("customerId") Integer customerId) {
		Map<String, Integer> paramMap = new HashMap<String, Integer>();
		paramMap.put("customerId", customerId);
		try {
			return this.cardEntityService.getByMap(paramMap);
		} catch (BaseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping("/ajax/ajaxGetCardEntityByCustomerAndRand")
	public @ResponseBody CardEntityVO ajaxGetCardEntityByCustomerAndRand(@RequestParam("customerId") Integer customerId) {
		try {
			return this.cardEntityService.selectByCustomerAndRand(customerId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//返回YES表示可以删
	@RequestMapping("/ajax/ajaxGetRefData")
	public @ResponseBody CommonDelResBean ajaxGetRefData(@RequestParam("ids") String ids, @RequestParam("tbName") String tbName) {
		TabRefBean table = (TabRefBean) applicationContext.getBean(tbName);
		table.setIds(ids);
		try {
			//true 表示可以删
			return this.commonDelService.getRefCount(table);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public IRolesService getRolesService() {
		return rolesService;
	}

	public void setRolesService(IRolesService rolesService) {
		this.rolesService = rolesService;
	}

	public IPermissionsService getPermissionsService() {
		return permissionsService;
	}

	public void setPermissionsService(IPermissionsService permissionsService) {
		this.permissionsService = permissionsService;
	}

	public ICustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public ICardCountInitService getCardCountInitService() {
		return cardCountInitService;
	}

	public void setCardCountInitService(
			ICardCountInitService cardCountInitService) {
		this.cardCountInitService = cardCountInitService;
	}

	public IMsgTemplateService getMsgTemplateService() {
		return msgTemplateService;
	}

	public void setMsgTemplateService(IMsgTemplateService msgTemplateService) {
		this.msgTemplateService = msgTemplateService;
	}

	public IServiceInitService getServiceInitService() {
		return serviceInitService;
	}

	public void setServiceInitService(IServiceInitService serviceInitService) {
		this.serviceInitService = serviceInitService;
	}

	public IRechargeRuleService getRechargeRuleService() {
		return rechargeRuleService;
	}

	public void setRechargeRuleService(IRechargeRuleService rechargeRuleService) {
		this.rechargeRuleService = rechargeRuleService;
	}

	public IUpgradeRuleService getUpgradeRuleService() {
		return upgradeRuleService;
	}

	public void setUpgradeRuleService(IUpgradeRuleService upgradeRuleService) {
		this.upgradeRuleService = upgradeRuleService;
	}

	public ICardLevelService getCardLevelService() {
		return cardLevelService;
	}

	public void setCardLevelService(ICardLevelService cardLevelService) {
		this.cardLevelService = cardLevelService;
	}

	public ICardAgioService getCardAgioService() {
		return cardAgioService;
	}

	public void setCardAgioService(ICardAgioService cardAgioService) {
		this.cardAgioService = cardAgioService;
	}

	public IRegioninfoService getRegioninfoService() {
		return regioninfoService;
	}

	public void setRegioninfoService(IRegioninfoService regioninfoService) {
		this.regioninfoService = regioninfoService;
	}

	public ICostService getCostService() {
		return costService;
	}

	public void setCostService(ICostService costService) {
		this.costService = costService;
	}

	public IMembercardServiceService getMembercardServiceService() {
		return membercardServiceService;
	}

	public void setMembercardServiceService(
			IMembercardServiceService membercardServiceService) {
		this.membercardServiceService = membercardServiceService;
	}

	public UserVO getSessionUser() { 
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		return (UserVO) request.getSession().getAttribute(
				SysConstants.SESSION_USER);
	}
}
